import com.google.appengine.api.datastore.Entity
import cn.xiazhengxin.blog.util.Gravatar as MD5Util
import cn.xiazhengxin.blog.util.ClientInfo as Client
import cn.xiazhengxin.blog.util.ThirdUtils as tu
import javax.servlet.http.Cookie
import java.util.Random

def entity = new Entity("comment")

def rand = new Random()

entity.cId = rand.nextLong().toString()

entity << params

def ua = headers["User-Agent"]

entity.os = Client.getOS(ua)

entity.browser = Client.getBrowser(ua)

if(entity.website == ""){
	entity.website = "http://g.cn"
}

def cookies = request.getCookies()

if(!cookies){
	
	response.addCookie(new Cookie("name", params.name))
	response.addCookie(new Cookie("email", params.email))
	response.addCookie(new Cookie("web", params.website))
}

entity.gravatarHash = MD5Util.md5Hex(params.email)

entity.pubTime = tu.getFormattedTime() 

entity.save()

request['message'] = "评论添加成功!"

forward "singleBlog.groovy?id=${params.aId}"